package it.polimi.reTraveldream.eao;

import java.util.ArrayList;
import java.util.List;

import it.polimi.reTravelDream.dto.ExcursionDTO;
import it.polimi.reTravelDream.dto.FlightDTO;
import it.polimi.reTravelDream.dto.HotelDTO;
import it.polimi.reTravelDream.dto.PrenotationDTO;
import it.polimi.reTravelDream.dto.TravelPackageDTO;
import it.polimi.reTravelDream.dto.UserDTO;
import it.polimi.reTravelDream.eao.PrenotationEAOLocal;
import it.polimi.reTraveldream.entities.Excursion;
import it.polimi.reTraveldream.entities.Flight;
import it.polimi.reTraveldream.entities.Hotel;
import it.polimi.reTraveldream.entities.Prenotation;
import it.polimi.reTraveldream.entities.TravelPackage;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;


@Stateless
public class PrenotationEAO implements PrenotationEAOLocal{

	
	@PersistenceContext
    private EntityManager em;
	


	@Override
	public List<PrenotationDTO> getUserPrenotation(UserDTO user) {
		TypedQuery<Prenotation> query= em.createNamedQuery(Prenotation.FIND_USER_PRENOTATIONS, Prenotation.class);
		query.setParameter("userName",user.getEmail());
		List<Prenotation> resultEntities=query.getResultList();
		List<PrenotationDTO> resultDTO= new ArrayList<PrenotationDTO>();
		 
		for (Prenotation p: resultEntities){
			 resultDTO.add(this.convertToDTO(p));
			 }
			return resultDTO;
		 
	}

	@Override
	public List<PrenotationDTO> getAllPrenotation() {
		List<Prenotation> resultEntities= em.createNamedQuery(Prenotation.FIND_ALL, Prenotation.class)
                .getResultList();
		 List<PrenotationDTO> resultDTO= new ArrayList<PrenotationDTO>();
		 for (Prenotation p: resultEntities){
			 resultDTO.add(convertToDTO(p));
		 }
		 return resultDTO;
	}

	@Override
	public void makeReservation(PrenotationDTO prenotationDTO) {
		System.out.println("prenotation");
		Flight outGoingFlightEntityFlight=em.find(Flight.class, prenotationDTO.getDepartureFlight().getId());
		Flight returnFlightEntity=em.find(Flight.class, prenotationDTO.getReturnFlight().getId());
		Hotel hotelChosedEntity=em.find(Hotel.class, prenotationDTO.getHotelChosen().getId());
		Excursion excursionEntity=em.find(Excursion.class, prenotationDTO.getExcursioneChosen().getId());
		String user=prenotationDTO.getClient().getEmail();
		
		Prenotation prenotation=new Prenotation(user,
				prenotationDTO.getPackageName(),
				outGoingFlightEntityFlight,
				returnFlightEntity,
				hotelChosedEntity, 
				excursionEntity,
				prenotationDTO.getPrice());
		this.em.persist(prenotation);
	}
	
	private PrenotationDTO convertToDTO(Prenotation prenotation){
		PrenotationDTO prenotationDTO=new PrenotationDTO();
		prenotationDTO.setPackageName(prenotation.getPackageName());
		prenotationDTO.setDepartureFlight(convertToDTO(prenotation.getOutGoingFlight()));
		prenotationDTO.setReturnFlight(convertToDTO(prenotation.getReturnFlight()));
		prenotationDTO.setHotelChosen(convertToDTO(prenotation.getHotelChosed()));
		prenotationDTO.setExcursioneChosen(convertToDTO(prenotation.getExcursionChosed()));
		prenotationDTO.setPrice(prenotation.getPrice());
		return prenotationDTO;
	}
	
	 private HotelDTO convertToDTO(Hotel hotelEntity) {
			HotelDTO hotelDTO = new HotelDTO();
			hotelDTO.setId(hotelEntity.getId());
			hotelDTO.setAddress(hotelEntity.getAddress());
			hotelDTO.setDescription(hotelEntity.getDescription());
			hotelDTO.setName(hotelEntity.getName());
			hotelDTO.setStars(hotelEntity.getStars());
			hotelDTO.setPrice(hotelEntity.getPrice());
			hotelDTO.setCity(hotelEntity.getCity());
			return hotelDTO;
			
		}
	 
	 private FlightDTO convertToDTO(Flight hotelEntity) {
			FlightDTO flightDTO = new FlightDTO();
			flightDTO.setId(hotelEntity.getId());
			flightDTO.setArrivalCity(hotelEntity.getArrivalAirport());
			flightDTO.setDepartureCity(hotelEntity.getDepartureAirport());
			flightDTO.setData(hotelEntity.getDate());
			flightDTO.setPrice(hotelEntity.getPrice());
			return flightDTO;
			
		}
	 
	 private ExcursionDTO convertToDTO(Excursion excursionEntity) {
			ExcursionDTO excursionDTO = new ExcursionDTO();
			excursionDTO.setId(excursionEntity.getId());
			excursionDTO.setName(excursionEntity.getName());
			excursionDTO.setDescription(excursionEntity.getDescription());
			excursionDTO.setPrice(excursionEntity.getPrice());
			return excursionDTO;
			
			
		}

}
